package com.ethink.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ethink.order.api.enums.TocSaleReturnImportLogResultReturnStatusEnum;
import com.ethink.order.api.info.TocSaleReturnImportLogResultInfo;
import com.ethink.order.api.request.TocSaleReturnImportLogResultAddRequest;
import com.ethink.order.api.request.TocSaleReturnImportLogResultSettleRequest;
import com.ethink.order.common.entity.TocSaleReturnImportLogResult;
import com.ethink.order.common.mapper.TocSaleReturnImportLogResultMapper;
import com.ethink.order.service.TocSaleReturnImportLogResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TOC销售退单结果 Service接口实现
 *
 * @author default
 * @date 2023-01-13
 */
@Slf4j
@Service
public class TocSaleReturnImportLogResultServiceImpl implements TocSaleReturnImportLogResultService {

    @Autowired
    private TocSaleReturnImportLogResultMapper tocSaleReturnImportLogResultMapper;


    @Override
    public Boolean batchInsert(List<TocSaleReturnImportLogResultAddRequest> addRequestList) {
        for (TocSaleReturnImportLogResultAddRequest addRequest : addRequestList) {
            addTocSaleReturnImportLogResult(addRequest);
        }
        return true;
    }

    @Override
    public Boolean addTocSaleReturnImportLogResult(TocSaleReturnImportLogResultAddRequest request) {
        TocSaleReturnImportLogResult tocSaleReturnImportLogResult = new TocSaleReturnImportLogResult();
        BeanUtils.copyProperties(request, tocSaleReturnImportLogResult);
        return tocSaleReturnImportLogResultMapper.insert(tocSaleReturnImportLogResult) > 0;
    }

    @Override
    public List<TocSaleReturnImportLogResultInfo> queryListByLogId(Long id) {
        return tocSaleReturnImportLogResultMapper.queryListByLogId(id);
    }

    @Override
    public void settleOrder(TocSaleReturnImportLogResultSettleRequest settleRequest) {
        tocSaleReturnImportLogResultMapper.update(null, new LambdaUpdateWrapper<TocSaleReturnImportLogResult>()
                .eq(TocSaleReturnImportLogResult::getShopOrderNo, settleRequest.getShopOrderNo())
                .set(TocSaleReturnImportLogResult::getReturnStatus, TocSaleReturnImportLogResultReturnStatusEnum.S2.getIntValue())
                .set(TocSaleReturnImportLogResult::getOrderNo,settleRequest.getOrderNo())
                .set(TocSaleReturnImportLogResult::getReturnOrderNo,settleRequest.getReturnOrderNo())
        );
    }
}
